<template>
    <div>
        <zm-panel title="岗位详情" :visible.sync="value" @change="close">
            <zm-form label-width="100px" :model="form" ref="zmForm">
                <zm-form-item label="岗位名称">
                    <span>{{form.name}}</span>
                </zm-form-item>
                <zm-form-item label="数据权限">
                    <span>{{form.dateAuthName}}</span>
                    <div v-if="form.dateAuth === '4'">
                        <div>所属公司：{{ form.companyName }}</div>
                        <div>所属部门：{{form.dateApartmentIdName}}</div>
                    </div>
                </zm-form-item>
                <zm-form-item label="菜单权限" prop="treeList">
                    <permission-tree v-if="treeList && treeList.length > 0" v-model="form.menuIds" :tree="treeList" :is-checkbox="false"></permission-tree>
                </zm-form-item>
            </zm-form>
            <div slot="footer" class="tr">
                <el-button @click="close">返回</el-button>
            </div>
        </zm-panel>
    </div>
</template>

<script>
import PermissionTree from '@/components/PermissionTree'
import panelMixin from '@/mixins/panelMixin'
import systemRoleApis from '@/api/src/systemRole'
import systemMenu from '@/api/src/systemMenu'

export default {
  components: { PermissionTree },
  mixins: [panelMixin],
  props: {
    rowInfo: {
      type: Object,
      default () {
        return {}
      }
    }
  },
  data () {
    return {
      form: {
        name: '',
        dateAuth: '',
        dateApartmentIdName: '',
        menuIds: []
      },
      treeList: [],
      menuIds: []
    }
  },
  created () {
    this.getDetail()
  },
  methods: {
    async getDetail () {
      const _res = await systemRoleApis.getInfo({ id: this.rowInfo.id })
      if (_res && _res.menuIds) {
        this.menuIds = _res.menuIds.split(',')
      }
      this.form = _res
      this.getPermissionTree(_res.menuIds)
    },
    findLastChild (list) {
      if (list && list.length > 0) {
        for (let len = list.length; len--;) {
          list[len].hasChildren = !!list[len].children
          list[len].isShow = this.menuIds.includes(list[len].id)
          if (list[len].position === '3') {
            if (list[len].children && list[len].children.length > 0) {
              list[len].children = list[len].children.filter(action =>
                this.menuIds.includes(action.id)
              )
            }
          } else {
            this.findLastChild(list[len].children)
          }
          if (!list[len].isShow) {
            list.splice(len, 1)
          }
        }
      }
    },
    async getPermissionTree (menuIds) {
      let _res = []
      _res = await systemMenu.getMenuTree({
        menuIds: menuIds
      })
      this.treeList = _res || []
    }
  }
}
</script>
